package com.song.JavaThread_day04.ch4_11;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TaskMain {
	
	
	public static void main(String[] args) {
		
		ExecutorService executor =(ExecutorService)Executors.newCachedThreadPool();
		CompletionService<String> service = new ExecutorCompletionService<>(executor);
		
		ReportRequest facerequest = new ReportRequest("face", service);
		ReportRequest onrequest = new ReportRequest("online", service);
		Thread fthread  = new Thread(facerequest);
		Thread othread  = new Thread(onrequest);
		
		ReportProcessor processor = new ReportProcessor(service);
		Thread pthread = new Thread(processor);
		
		System.out.printf("Main : Starting the threads \n");
		
		fthread.start();
		othread.start();
		pthread.start();
		
		try {
			System.out.printf(" Main waiting for the report generators \n");
			fthread.join();
			othread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.printf("main : shutting down the executor \n");
		executor.shutdown();
		
		try {
			executor.awaitTermination(1, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		processor.setEnd(true);
		
		System.out.println("Main : Ends");
	}
}
